package com.songlanyun.modules.usergains.entity;

import cn.hutool.system.UserInfo;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.math.BigDecimal;
import java.io.Serializable;
import java.util.Date;

import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.*;
import com.songlanyun.common.utils.SpringContextUtils;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.account.entity.UserInfoEntity;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.gains.entity.ShopOrderGains;
import com.songlanyun.modules.gains.service.ShopOrderGainsService;
import com.songlanyun.modules.rabbitmq.util.SpringContextUtil;
import com.songlanyun.modules.userbankcard.entity.UserBankCardEntity;
import com.songlanyun.modules.userbankcard.service.UserBankCardService;
import lombok.Data;

/**
 * 会员分佣所得表
 *
 * @author wuchao
 * @email sunlightcs@gmail.com
 * @date 2020-12-23 11:48:16
 */
@Data
@TableName("yxt_distribution_user_order_gains")
public class UserOrderGains extends RootEntity<Long, UserOrderGains> implements Serializable {
    private static final long serialVersionUID = 1L;

    {
        settlement = false;
        ableSettlement = false;
    }

    /**
     *
     */
    @TableId
    private Long id;
    /**
     * 订单id
     */
    private Long orderId;
    /**
     * 订单编号  --1
     */
    private String orderCode;
    /**
     * 子订单编号
     */
    private Long childOrderId;
    /**
     * 0普通商品，1服务商品
     */
    private ShopGainsConstant.GoodsType goodsType;
    /**
     * 商品id
     */
    private Long goodsId;
    /**
     * 营销活动内类型(0无活动，1秒杀，2拼团）
     */
    private Integer marketingType;
    /**
     * 商家id
     */
    private Long shopId;
    /**
     * 商家名称
     */
    private String shopName;
    /**
     * 商品名称
     */
    private String goodsTitle;
    /**
     * 是否存在退款
     */
    private Boolean existsRefund;
    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    /**
     * 最后一次的计算时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
    /**
     * 下单会员id
     */
    private Long userId;

    private OrderConstant.UserOrderGainsType userOrderGainsType= OrderConstant.UserOrderGainsType.GENERAL;



    @TableField(exist = false)
    private UserInfoEntity userInfo;

    private UserInfoService loadUserInfoService() {
        return (UserInfoService) SpringContextUtil.getBean("yxtUserInfoService");
    }

    public UserInfoEntity loadUserInfo() {
        if (userInfo == null) {
            UserInfoService userInfoService = loadUserInfoService();
            userInfo = userInfoService.loadById(getUserId(), false);
        }
        return userInfo;
    }

    /**
     * 商品短标题
     */
    private String shortGoodsTitle;
    /**
     * 最终商品单价
     */
    private BigDecimal realPrice;
    /**
     * 原规格销售价
     */
    private BigDecimal salePrice;
    /**
     * 商品图片
     */
    private String goodsImg;
    /**
     * 总实际价格
     */
    private BigDecimal totalRealPrice;
    /**
     * 出货方式（0物流，1自提）
     */
    private OrderConstant.ShipmentMode shipmentMode;
    /**
     * 单位
     */
    private String unit;
    /**
     * 商品规格编码
     */
    private String skuBarCode;
    /**
     * 规格的Hash编码，方便去对应和查找
     */
    private String skuHashCode;
    /**
     * 规格集的文本显示值
     */
    private String skuSetName;
    /**
     * 数量
     */
    private Integer number;
    /**
     * 商品价总优惠多少钱
     */
    private BigDecimal totalReduced;
    /**
     * 首单优惠分摊多少钱
     */
    private BigDecimal firstOrderReduced;

    /**
     * 总促销优惠
     *
     * @return
     */
    public BigDecimal getTotalPromotionReduced() {
        return MathUtils.add(totalReduced, firstOrderReduced);
    }


    /**
     * 单位成本
     */
    private BigDecimal cost;
    /**
     * 总成本
     */
    private BigDecimal totalCost;
    /**
     * 单品标识
     */
    private Long singleGoodsId;
    /**
     * 可得总价
     */
    private BigDecimal gains;

    /**
     * 退款扣除
     */
    private BigDecimal refundDeduct;

    /**
     * 实际所得（是扣出退款）
     */
    private BigDecimal ableGains;


    /**
     * 计算实际所得 TODO之后需要加入分润
     */
    public void computeAbleGains() {
        this.ableGains = MathUtils.subtract(this.getGains(), this.getRefundDeduct());
    }

    /**
     * 结算时间
     */
    private Date settlementTime;
    /**
     * 是否已结算
     */
    private Boolean settlement;

    /**
     * 分佣会员id
     */
    private Long distributionUserId;

    /**
     * 分佣会员昵称
     */
    @TableField(exist = false)
    private String distributionUserName;

    /**
     * 单件需要赠送多少贡献值
     */
    private BigDecimal consumptionValue= BigDecimal.ZERO ;


    public void loadDistributionUserName() {
        UserInfoService userInfoService = loadUserInfoService();
        UserInfoEntity userInfo = userInfoService.getById(getDistributionUserId());
        if (userInfo != null) {
            distributionUserName = userInfo.loadRealName();
        }
    }

    /**
     * 分佣会员手机号  --2
     */
    private String distributionUserMobile;
    /**
     * 0\一二级分佣、1\282分佣、2\6750礼包分佣、3\差价分佣
     */
    private DistributionConstant.DistributionComputer distributionComputer;
    /**
     * 分佣等级code
     */
    private String distributionUserRoleCode;
    /**
     * 分佣等级名称
     */
    private String distributionUserRoleName;
    /**
     * 0,自营商品1级分佣，
     * <p>
     * 1,自营商品2级分佣，
     * <p>
     * 2,代销商品小程序分佣282上一级分佣
     * <p>
     * 3,代销商品小程序分佣282上两级分佣
     * <p>
     * 4,代销商品小程序分佣282最近的市代分佣
     * <p>
     * 5,代销商品小程序分佣6750，直接上级平台合伙人分佣
     * 6,代销商品小程序分佣6750，间接上级县级代理或市级代理分佣
     * 7,代销商品小程序分佣6750，直接上级县级代理或市级代理分佣
     * 8,代销商品小程序分佣6750，县级代理或市级代理10%分佣
     * 9,代销商品小程序分佣6750，县级代理或市级代理7%分佣
     * <p>
     * 10,代销商品小程序差价分佣，一级差价分佣
     * 11,代销商品小程序差价分佣，二级差价分佣
     * 12,代销商品小程序差价分佣，县级代理或市级代理10%分佣
     * 13,代销商品小程序差价分佣，县级代理或市级代理7%分佣
     */
    private DistributionConstant.DistributionType distributionType;
    /**
     * 分佣说明
     */
    private String envRemark;
    /**
     * 是否是代销
     */
    private Boolean proxyGoods;
    /**
     * 渠道类型
     */
    private OrderConstant.ChannelType channelType;
    /**
     * 礼包类型
     */
    private GoodsConstant.GiftType giftType;
    /**
     * 结算说明
     */
    private String settlementRemark;
    /**
     * 是否可以结算了  --3
     */
    private Boolean ableSettlement;
    /**
     * 应该结算时间
     */
    private Date shouldSettlementTime;
    /**
     * 商家所得记录id
     */
    private Long shopGainsId;

    /**
     * 订单状态
     * 订单未完成
     * 订单已完成，未结算
     * 订单已结算
     */
    @TableField(exist = false)
    private String orderStatus;

    public String getOrderStatus() {
        if (settlement) {
            return "已结算";
        } else if (!ableSettlement) {
            return "未完成";
        } else {
            return "已完成，未结算";
        }

    }

    @Override
    public Long getIdentity() {
        return id;
    }


    public void computeRefundDeduct(ShopOrderGains gains) {
        if (this.getGains().compareTo(BigDecimal.ZERO) == 0) {
            return;
        } else {
            if (gains.getGains().compareTo(BigDecimal.ZERO) == 0) {
                return;
            } else {
                this.refundDeduct = MathUtils.setScale(gains.getRefundMoney().doubleValue() * this.getGains().doubleValue() / gains.getGains().doubleValue());
                this.computeAbleGains();
                this.setEnvRemark(this.getEnvRemark() + ",因退款实际所得贡献值" + this.getAbleGains());
            }

        }
    }

    @TableField(exist = false)
    private UserBankCardEntity userBankCard;

    public UserBankCardEntity loadUserBankCard() {
        if (userBankCard == null) {
            UserBankCardService userBankCardService = (UserBankCardService) SpringContextUtil.getBean("userBankCardService");
            userBankCard = userBankCardService.defaultBankCard(MemberConstant.UserType.MEMBER, this.getDistributionUserId());
        }
        return userBankCard;
    }

    /**
     * 分润比率
     */
    private BigDecimal ratio = BigDecimal.ZERO;


    @TableField(exist = false)
    private BigDecimal shopAbleGains = BigDecimal.ZERO;
    public void loadShopAbleGains()
    {
        ShopOrderGainsService shopOrderGainsService = SpringContextUtils.getBean("shopOrderGainsService", ShopOrderGainsService.class);
        ShopOrderGains shopOrderGains = shopOrderGainsService.getById(this.getShopGainsId());
        if(shopOrderGains.getGains().compareTo(shopOrderGains.getRefundMoney())>0)
        {
            shopAbleGains = MathUtils.setScale(shopOrderGains.getGains().doubleValue() - shopOrderGains.getRefundMoney().doubleValue());
        }
    }

    /**
     * 余额分润所得
     */
    private BigDecimal balanceGains = BigDecimal.ZERO;


    /**
     * 余额分润比率
     */
    private BigDecimal balanceRatio = BigDecimal.ZERO;

}
